﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using System.Data;
using Comm;
using BusinessFacade;
using Entities;

namespace MedicareNoteManagerWeb.Web
{
    public partial class NoteMoubleAdd : BasePage
    {
        public static string ID = "";
        nm_NoteMouble_Model model1 = new nm_NoteMouble_Model();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                model1 = (nm_NoteMouble_Model)Session["NotemessageModel"];
                BindDrop();
                try
                {
                    ID = "";
                    ID = Request.QueryString["ID"].ToString();
                }
                catch (Exception ex)
                {
                    labMessage.Text = ex.Message;
                    labMessage.Text = "";
                }
                if (ID != "")
                {
                    FillValue();
                }
            }
        }


        /// <summary>
        /// 修改填充值
        /// </summary>
        public void FillValue()
        {
            Button1.Text = "修改";
            Busi_nm_NoteMouble u = new Busi_nm_NoteMouble();
            nm_NoteMouble_Model model = u.GetListModel(Convert.ToInt32(ID));
            txt_MoubleName.Text = model.MoubleName;
            DropArea.SelectedValue = model.MoubleArea;
            txt_Content.Text = model.MoubleContent;
            txt_Explain.Text = model.MoubleExplain;
        }

        /// <summary>
        /// 绑定区域
        /// </summary>
        public void BindDrop()
        {
            FillDropDownList("GetDrop", "AreaID", "AreaName", "请选择区域", this.DropArea);
        }

        /// <summary>
        /// 验证值
        /// </summary>
        /// <returns></returns>
        public string CheckData()
        {
            string strErr = "";
            if (string.IsNullOrEmpty(txt_MoubleName.Text.Trim()))
            {
                strErr += "模板名称不为空";
                return strErr;
            }
            if (DropArea.SelectedValue == "0")
            {
                strErr += "请选择区域";
                return strErr;
            }
            if (string.IsNullOrEmpty(txt_Content.Text.Trim()))
            {
                strErr += "模板内容不为空";
                return strErr;
            }
            if (string.IsNullOrEmpty(txt_Explain.Text.Trim()))
            {
                strErr += "模板说明不为空";
                return strErr;
            }
            return strErr;
        }

        /// <summary>
        /// 清空文本值
        /// </summary>
        protected void ClearValue()
        {
            DropArea.SelectedIndex = 0;
            txt_MoubleName.Text = "";
            txt_Content.Text = "";
            txt_Explain.Text = "";
        }



        /// <summary>
        /// 添加和修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button4_Click(object sender, EventArgs e)
        {
            sys_user_Model user = new sys_user_Model();
            model1 = (nm_NoteMouble_Model)Session["NotemessageModel"];
            string strErr = CheckData();
            if (string.IsNullOrEmpty(strErr))
            {
                if (ID == "")
                {
                    nm_NoteMouble_Model model = new nm_NoteMouble_Model();
                    model.MoubleArea = int.Parse(DropArea.SelectedValue).ToString();
                    model.MoubleName = txt_MoubleName.Text;
                    model.MoubleContent = txt_Content.Text;
                    model.MoubleExplain = txt_Explain.Text;
                    model.AuditingPerson = null;
                    model.AuditingState = 0;
                    model.AuditingSuggestion = null;
                    model.CreatePersonID = Convert.ToInt32(login.UserID);
                    model.CreateTime = DateTime.Now;
                    Busi_nm_NoteMouble users = new Busi_nm_NoteMouble();
                    DataTable ds = users.GetList(model.MoubleName,-1);
                    if (!(ds == null || ds.Rows.Count == 0))
                    {
                        MessageBox.ShowDialog(Page, "模板名称已存在！", "error");
                        return;
                    }
                    if (new Busi_nm_NoteMouble().Add(model))
                    {
                        MessageBox.ShowDialog(this.Page, "添加成功！", "success");
                        ClearValue();
                        return;
                    }
                    else
                    {
                        MessageBox.ShowDialog(this.Page, "添加失败！", "error");
                        return;
                    }
                }
                else
                {
                    //修改   
                    try
                    {
                        nm_NoteMouble_Model model = new nm_NoteMouble_Model();
                        model.ID = Convert.ToInt32(ID);
                        model.MoubleArea = DropArea.SelectedValue;
                        model.MoubleName = txt_MoubleName.Text;
                        model.MoubleContent = txt_Content.Text;
                        model.MoubleExplain = txt_Explain.Text;
                        model.UpdatePersonID = user.UserID;
                        model.UpdateTime = DateTime.Now;
                        Busi_nm_NoteMouble users = new Busi_nm_NoteMouble();
                        DataTable ds = users.GetList(model.MoubleName, -1);
                        if (!(ds == null || ds.Rows.Count == 0))
                        {
                            MessageBox.ShowDialog(Page, "模板名称已存在！", "error");
                            return;
                        }
                        else
                        {
                            new Busi_nm_NoteMouble().Update(model);
                            MessageBox.ShowDialog(Page, "修改成功！", "success");
                            ClearValue();
                            return; 
                        }
                    }
                    catch (Exception ex)
                    {
                        labMessage.Text = ex.Message;
                        labMessage.Text = "";
                        MessageBox.ShowDialog(Page, "修改失败！", "error");
                        return;
                    }
                }
            }
            else
            {
                labMessage.Text = strErr;
            }
        }
    }
}